/**
 * Created with IntelliJ IDEA.
 * User: mkadush
 * Date: 13.09.13
 * Time: 11:44
 * To change this template use File | Settings | File Templates.
 */

//--------------------------------------------------
// function to get selectedForm by prefix
//--------------------------------------------------

var getCurrentForm = function(prefixId){

    var currentForm=null;
    switch (prefixId) {
        case'org_':
            currentForm = Ext.getCmp('formOrganization');
            break;
        case 'dep_':
            currentForm = Ext.getCmp('formDepartment');
            break;
        case'pro_':
            currentForm = Ext.getCmp('formProject');
            break;
        case'hum_':
            currentForm = Ext.getCmp('formPeople');
    }
    return currentForm;
};



//--------------------------------------------------
// Handler for click on a tree node
//--------------------------------------------------

var onItemClick = function (node, evt) {


    var prefixId = node.attributes.id.substring(0, 4);

    var saveButtonPanel = Ext.getCmp('saveButtonPanel');

    var currentForm = getCurrentForm(prefixId);

    var centerRegion = Ext.getCmp('centerRegion');
    for (var i= 0; i< centerRegion.items.getCount(); i++){
        centerRegion.items.itemAt(i).hide()
    }

    if (currentForm) {
        if (prefixId != 'org_') {
            var id = node.attributes.id;
            currentForm.getForm().load({
                url: '../Organization/loadDataToForm',
                params: { id: id  },
                success: function (form, action) {
                },
                failure: function (form, action) {
                    Ext.MessageBox.alert('Bad ');
                }
            });

            saveButtonPanel.show();

        } else {
            var nameOrg = currentForm.getForm().findField('name');
            nameOrg.setValue(node.attributes.text);

            saveButtonPanel.hide();
        }
        currentForm.show();

        var saveButton = Ext.getCmp('saveButton');
        saveButton.removeListener('click', onCompleteAdd);
        saveButton.addListener('click', onButtonSaveClick);
        saveButton.setText('Сохранить');

    }
};

//--------------------------------------------------
// Handler for click on the save button
//--------------------------------------------------

var onButtonSaveClick = function (btn, e) {
    var tree = Ext.getCmp('treepanel');
    var selectedNode = tree.getSelectionModel().getSelectedNode();
    var prefixId = selectedNode.attributes.id.substring(0, 4);
    var currentForm = getCurrentForm(prefixId);

    currentForm.getForm().submit({
        params: {
            id: selectedNode.attributes.id
        },
        url: '../Organization/saveDataFromForm',
        success: function (form, action) {
            selectedNode.setText(currentForm.getForm().findField('name').getValue());
            Ext.MessageBox.alert(action.result.msg);
        },
        failure: function (form, action) {
             Ext.MessageBox.alert(action.result.msg);
        }
    });
};
